package com.cskaoyan._01btyestream.copy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/17 16:41
 **/
/*
文件复制功能
 */
public class Demo {
    public static void main(String[] args) throws IOException {
        // 创建输入流对象
        // 文本文件  71ms  1ms
        // 图片文件  1153ms  3ms
        // 视频文件  11800ms  22ms
        //FileInputStream in = new FileInputStream("D:\\a.txt");
        //FileInputStream in = new FileInputStream("D:\\a.jpg");
        FileInputStream in = new FileInputStream("D:\\aa.mp4");

        // 创建输出流对象
        //FileOutputStream out = new FileOutputStream("copy_a.txt");
        //FileOutputStream out = new FileOutputStream("copy_a.jpg");
        FileOutputStream out = new FileOutputStream("copy_aa.mp4");

        long start = System.currentTimeMillis();
        // 边读边写
        // 单字节复制
        //copy1(in, out);

        // 字节数组方式方式
        copy2(in, out);

        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");


        // 释放资源
        in.close();
        out.close();
    }

    private static void copy2(FileInputStream in, FileOutputStream out) throws IOException {
        int readCount;
        byte[] bytes = new byte[1024];
        while (( readCount = in.read(bytes)) != -1) {
            // 写操作
            out.write(bytes, 0, readCount);
        }
    }

    private static void copy1(FileInputStream in, FileOutputStream out) throws IOException {
        int readData;
        while ((readData = in.read()) != -1) {
            // 写入操作
            out.write(readData);
        }
    }
}
